// pages/details/details.js
var WxParse = require('../../lib/wxParse/wxParse.js');
import util from "../../utils/util"
import Api from "../../utils/api"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        info: {
            num: 11,
            type: "TW_233"
        },
        id: 0,
        goods:{},
        number:1,
        actions: [
            {
                name: 'TW_232'
            },
            {
                name: 'TW_233'
            }, {
                name: 'TW_234'
            }
        ]
    },
    //立即购买（先自动加入购物车,然后跳转至确认订单界面payInfo）
    onClickButton() {
        wx.navigateTo({
            url: '../payInfo/payInfo',
        })
    },
    addToCart: function () {
        //添加到购物车
        util.request(Api.wx.CartAdd, {
            goodsId: this.data.goods.id,
            number: this.data.number,
            productId: 0
        }, "POST")
            .then(function (res) {
                let _res = res;
                if (_res.errno == 0) {
                    wx.showToast({
                        title: '添加成功'
                    });
                    // that.setData({
                    //     openAttr: !that.data.openAttr,
                    //     cartGoodsCount: _res.data
                    // });
                    // if (that.data.userHasCollect == 1) {
                    //     that.setData({
                    //         collect: true
                    //     });
                    // } else {
                    //     that.setData({
                    //         collect: false
                    //     });
                    // }
                } else {
                    util.showErrorToast(_res.errmsg);
                }

            });
    },
    onSelect(event) {
        this.setData({
            info: {
                type: event.detail.name,
                num: this.data.info.num
            }
        });
    },
    showSheet: function () {
        this.setData({show: true});
    },
    onClose: function () {
        this.setData({show: false});
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        if (options.id) {
            that.setData({
                id: parseInt(options.id)
            });
        }
        this.getGoodsInfo();
    },
    getGoodsInfo:function(){
        let that = this;
        util.request(Api.wx.GoodsDetail, {
            id: that.data.id
        }).then(function (res) {
            if (res.errno === 0) {
                that.setData({
                    goods: res.data.info
                });
                WxParse.wxParse('goodsDetail', 'html', res.data.info.detail, that);
            }
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})